'use strict'
const TransformPages = require('uni-read-pages')
const CopyPlugin = require('copy-webpack-plugin')
const { webpack } = new TransformPages()
const path = require('path')
require('events').EventEmitter.defaultMaxListeners = 0
const ossHostMap = {
  dev: '//mall-sld-test.oss-cn-beijing.aliyuncs.com',
  test: '//mall-sld-test.oss-cn-beijing.aliyuncs.com',
  prod: '//mall-sld-prod.oss-cn-beijing.aliyuncs.com',
}
// eslint-disable-next-line no-multi-assign
const ossHost = process.env.VUE_APP_OSS_HOST = ossHostMap[process.env.VUE_APP_API_ENV] || ossHostMap.prod
let imageUrl = ''
const localIgnore = process.env.NODE_ENV === 'production' || !process.env.VUE_APP_LOCAL_STATIC
if (localIgnore) {
  imageUrl = ossHost + '/local/'
} else {
  imageUrl = '/static/local/'
}
process.env.VUE_APP_IMAGE_URL = imageUrl


const name = ''

module.exports = {
  lintOnSave: process.env.NODE_ENV === 'development',
  configureWebpack: (config) => {
    // 路径别名
    config.name = name
    config.plugins.push(new webpack.DefinePlugin({
      ROUTES: webpack.DefinePlugin.runtimeValue(function() {
        const tfPages = new TransformPages({
          includes: ['path', 'name', 'aliasPath'],
        })
        return JSON.stringify(tfPages.routes)
      }, true),
    }))

    if (localIgnore) {
      // 不稳定，依赖包更新大概率gg
      const copyPlugin = config.plugins.find((plugin) => plugin.constructor.name === 'CopyPlugin')
      copyPlugin.patterns[0].ignore.push('local/**/*')
    }
    if (process.env.UNI_PLATFORM === 'h5') {
      config.plugins.push(new CopyPlugin([
        {
          from: path.resolve(config.context, 'public'),
          to: config.output.path,
          ignore: ['index.html'],
        },
      ]))
    }
  },
  css: {
    loaderOptions: {
      scss: {
        // $baseUrl scss里头写图片的地址，对比 getApp().globalData.imgUrl
        additionalData: `@import "@/uni.scss";$baseUrl: "${imageUrl}";`,
      },
    },
  },
}